#include <string.h>
//#include <stdlib.h>
//#include "PCB.H"
//#include "R2.h"

#define PROC_CLASS_SYS 0
#define PROC_CLASS_APP 1

typedef struct process_control_block {
	char name[9];
	//application or system
	int type; 
	//values between -128 and 127, inclusive. higher = greater priority
	int priority;
	//running, ready, or blocked state
	int state;
	//suspended or not suspended
	int suspended; 
	
	//unsigned char stack_area[PROC_STACK_SIZE];
	//stack grows from high to low in 8086 architecture
	unsigned char *stack_top; 
	
	//initialize stack_top to highest address + 1, points to lowest occupied address
	//points to base
	unsigned char *stack_base; 
	
	//memory description information - used for instruction code and static data
	
	int mem_size;
	//start of mem area into which process to be loaded
	unsigned char *mem_load_address; 
	//address at which executing should begin
	unsigned char *mem_exec_address; 
	//pointer for next PCB in queue
	struct process_control_block *next; 
	//pointer to previous PCB in queue
	struct process_control_block *prev; 
} pcb;

void create2();

void create2() {
	pcb *new_pcb;
	new_pcb = (pcb*) sys_alloc_mem(sizeof(pcb));
		strcpy(new_pcb->name, "rex");
		new_pcb->priority = 1;
}

void create_pcb(char name[20], char type_string[], char priority_string[], char extra[]){
	char *msg3;
	pcb *new_pcb;
	new_pcb = (pcb*) sys_alloc_mem(sizeof(pcb));
		strcpy(new_pcb->name, "rex");
		new_pcb->priority = 1;

	msg3 = sys_alloc_mem(44);
	strcpy(msg3, "in createpcb");
	if(extra[0] == '\0'){
		int type_int = atoi(type_string);
		int priority_int = atoi(priority_string);
		if(check_name(name) == 0){
			if(type_int == PROC_CLASS_SYS || type_int == PROC_CLASS_APP){
				if(priority_int >= -128 && priority_int <= 127){
					printf(":%s:\n", msg3);
					new_pcb =  (pcb*) sys_alloc_mem( sizeof(pcb));
					strcpy(new_pcb->name, name);
					new_pcb->priority = priority_int;
					new_pcb->stack_top = NULL;
					//new_pcb = (pcb *)setup_PCB(name, type_int, priority_int);
					//insert_pcb(new_pcb, PROC_STATE_READY, PROC_NOT_SUSP);
				}
				else{
					printf("That is not a valid priority number\n");
				}
			}
			else{
				printf("This is not a valid process class\n");
			}
		}
		else{
			printf("That name is not unique\n");
		}
	}
	else{
		printf("The create_pcb command cannot use that many argument\n");
	}
}

void delete_pcb(char argument_1[], char argument_2[]){
	
}

void block_pcb(char argument_1[], char argument_2[]){
	
}

void unblock_pcb(char argument_1[], char argument_2[]){
	
}

void suspend_pcb(char argument_1[], char argument_2[]){
	
}

void resume_pcb(char argument_1[], char argument_2[]){
	
}

void set_priority_pcb(char argument_1[], char argument_2[]){
	
}

void show_pcb(char name[]){
	pcb *found = find_pcb(name);
	if(found == NULL){
		printf("PCB not found.");
	}
	else{
		//I seriously don't know if this is how this works or not :(
		printf("%s\n", found->name);
		if(found->state == 0){
			printf("Ready\n");
		}
		else if(found->state == 1){
			printf("Running\n");
		}
		if(found->suspended == 0){
			printf("Not suspended\n");
		}
		else if(found->suspended == 1){
			printf("Suspended\n");
		}
		printf("%d\n", found->priority);	
	}


}

void show_all(){
//traverse and print ready queue

//traverse and print blocked queue

}

void show_ready(){

}


void show_blocked(){

}